package com.softfuxi.tbs.app.feature;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.softfuxi.tbs.app.utils.LocationTools;
import com.softfuxi.tbs.app.utils.StartLocationTools;
import com.tencent.smtt.sdk.QbSdk;

public class X5WebViewActivity extends BaseWebViewActivity {

    private static final String M_TAG = "X5WebViewActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        this.setTAG(M_TAG);
        super.onCreate(savedInstanceState);
        startDefinedUrl();
//        makeItem("JS交互", X5WebViewActivity.class, "file:///android_asset/webpage/JSExamplePage.html"),
//        mWebView.loadUrl("file:///android_asset/webpage/main.html");
//                mWebView.loadUrl("http://192.168.3.39:8080/bc/index.html");
//        mWebView.loadUrl("http://192.168.2.202:3000");

//        initLocation();

    }

    @Override
    protected void initWebView() {
        super.initWebView();
//        Toast.makeText(this, mWebView.getIsX5Core() ? "X5内核: " + QbSdk.getTbsVersion(this) : "SDK系统内核" , Toast.LENGTH_SHORT).show();
    }

    private void initTest() {

        //声明AMapLocationClient类对象
        AMapLocationClient mLocationClient = null;
        //声明定位回调监听器
        AMapLocationListener mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                Log.e("TAG", "onLocationChanged getLongitude : " + aMapLocation.getLongitude() );
                Log.e("TAG", "onLocationChanged getLatitude : " + aMapLocation.getLatitude() );
            }
        };
        try {
            //初始化定位
            mLocationClient = new AMapLocationClient(getApplicationContext());
            //设置定位回调监听
            mLocationClient.setLocationListener(mLocationListener);
        } catch (Exception e) {
            Log.e("TAG", "initTest: " + e.toString() );
        }

    }

    //初始化定位信息
    private void initLocation() {
        Log.e("###", "initLocation 66666 ");
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Log.e("###", "ACCESS_FINE_LOCATION 66666 ");
        } else {
            Log.e("###", "ACCESS_FINE_LOCATION 1111 ");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 66);
        }
        AMapLocationClient.updatePrivacyAgree(this, true);
        AMapLocationClient.updatePrivacyShow(this, true, true);
        initTest();
        //初始化地址位置
        StartLocationTools.getInstance().initLocation(getApplicationContext(), new StartLocationTools.IResultListener() {
            @Override
            public void onLocation(AMapLocation aMapLocation) {
                Log.e("###", "location 2 lat => " + aMapLocation.getLatitude());
                Log.e("###", "location 2 long => " + aMapLocation.getLongitude());
                Log.e("###", "location 2 address => " + aMapLocation.getAddress());
//                CityBean mLocationCity = CityDataManager.getInstance().getCityByName(CityType.HOTEL, aMapLocation.getCity());
//                GlobalData.setMapLocation(aMapLocation);
//                if (null != mLocationCity) {
//                    GlobalData.setLocationCity(mLocationCity);
//                }
            }
        });

        new LocationTools(this).start(new LocationTools.OnLocationResultListener() {
            @Override
            public void onLocationSuccess(AMapLocation aMapLocation) {
                Log.e("###", "location 1 lat => " + aMapLocation.getLatitude());
                Log.e("###", "location 1 long => " + aMapLocation.getLongitude());
                Log.e("###", "location 1 address => " + aMapLocation.getAddress());
            }

            @Override
            public void onLocationFailed() {
                Log.e("###", "location 1 onLocationFailed");

            }
        });
    }

    private void startDefinedUrl() {
        Log.e("TAG", "startDefinedUrl: ");

        Intent intent = getIntent();
        if (intent != null) {
            String url = intent.getStringExtra("url");
            if (mWebView != null) {
                Log.e("TAG", "startDefinedUrl: " + url);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.loadUrl(url);
                    }
                });

            }
        } else {
            Log.i(M_TAG, "Intent is null");
        }
    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//
//    }


    //    @Override
//    onRequest//    public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
////        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        //
//        if (requestCode == 66) {
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
////                有权限
//                selectPicUtil.takePhoto();
//            } else {
////                无权限
//            }
//        }
//        if (requestCode == PHOTO_REQ_CODE) {
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
////                有权限
//                selectPicUtil.getPhoto();
//            } else {
////                无权限
//            }
//        }
//    }

}
